package com.chevip.article.sync.service;

import com.alibaba.fastjson.JSON;
import com.chevip.article.sync.Constant;
import com.chevip.article.sync.HttpRequester;
import com.chevip.article.sync.entity.primary.Article;
import com.chevip.article.sync.entity.primary.ArticleCategory;
import com.chevip.article.sync.entity.primary.ArticleContent;
import com.chevip.article.sync.entity.second.OldArticle;
import com.chevip.article.sync.repository.primary.ArticleCategoryRepository;
import com.chevip.article.sync.repository.primary.ArticleContentRepository;
import com.chevip.article.sync.repository.primary.ArticleRepository;
import com.chevip.article.sync.repository.second.OldArticleRepository;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ArticleService {
    @Autowired
    private OldArticleRepository oldArticleRepository;
    @Autowired
    private ArticleRepository articleRepository;
    @Autowired
    private ArticleContentRepository articleContentRepository;
    @Autowired
    private ArticleCategoryRepository articleCategoryRepository;

    private static final String address = "http://localhost/";

    public void syncArticle() {
        List<OldArticle> oldArticleList = Lists.newArrayList(oldArticleRepository.findAll());
        log.info(JSON.toJSONString(oldArticleList, true));
        for (OldArticle oldArticle : oldArticleList) {
            Article article = convertFromOldArticle(oldArticle);
            if (article != null) {
                Article db = articleRepository.save(article);

                String body = HttpRequester.sendGet(address + oldArticle.getFilePath());
                if (StringUtils.isNotBlank(body)) {
                    ArticleContent articleContent = new ArticleContent();
                    articleContent.setArticleId(db.getId());
                    body = body.replaceAll("src=\"../userfiles/upload", "src=\"" + address + "userfiles/upload");
                    articleContent.setContent(body);
                    articleContentRepository.save(articleContent);

                    String clean = Jsoup.clean(body, new Whitelist());
                    clean = clean.replace("&gt;", ">");
                    clean = clean.replace("&lt;", "<");
                    clean = clean.replace("&nbsp;", " ");
                    clean = clean.replace("&nbsp;", " ");
                    clean = clean.replace("&quot;", "\"");
                    clean = clean.replace("<br/> ", "\n");
                    clean = clean.substring(0, clean.length() < 180 ? clean.length() : 180);
                    db.setIntro(clean);
                    articleRepository.save(db);
                }
            }
        }
    }

    private Article convertFromOldArticle(OldArticle oldArticle) {
        Article article = new Article();
        article.setCreateTime(oldArticle.getCreateTime());
        article.setDisabled(false);
        article.setTitle(oldArticle.getArticleName());
        article.setModifiedTime(oldArticle.getModifyTime());
        article.setPublishTime(oldArticle.getPublicDate());
        article.setAuthor(oldArticle.getAuthor());
        article.setCategoryId(0L);
        article.setInputUserId(1L);

        Constant.ArticleType articleType = Constant.ArticleType.get(oldArticle.getArticleType());
        if (articleType == null) {
            return null;
        }
        ArticleCategory articleCategory = articleCategoryRepository.getByMarkCode(articleType.toString());
        article.setCategoryId(articleCategory.getId());

        article.setHits(oldArticle.getReadCount().longValue());
        if (StringUtils.isNotBlank(oldArticle.getTop())) {
            article.setTop(Boolean.parseBoolean(oldArticle.getTop()));
        }
        Constant.ArticleState state = Constant.ArticleState.get(oldArticle.getArticleState());
        if (state != null) {
            article.setState(state.getNewValue());
        }
        article.setRemark(oldArticle.getRemark());
        article.setSource(oldArticle.getSource());
        return article;
    }
}
